package gapp.gui;

import gapp.ctrl.ControlFactory;
import gapp.itg.PropertyChangeNames;
import gapp.itg.SubsysStateObservable;
import gapp.subsysstate.State;

import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("serial")
public class MainFrame extends JFrame implements PropertyChangeListener {
	private JTextField jTxtFldToggleState;
	private JButton jBtnService;
	private JTextField jTextFieldIn;
	private JPanel jPanelDummy;
	private JLabel jLabelIn;
	private JButton jBtnToggle;

	// This part is coded by hand ----------------------------------

	public MainFrame() {
		// Other solutions possible
		SubsysStateObservable.getAsObservable().addPropertyChangeListener(this);
		// Order matters
		initGUI();
		initCtrl();
		
		// Ugly, force GUI to show start state
		// Can't use observer at start
		String s = SubsysStateObservable.getInstance().getState().toString();
		jTxtFldToggleState.setText(s);
	}

	// Part of Observer pattern
	// Possible use invokeLater() if this is not the Swing EDT
	// calling.
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		System.out.println("MainFrame ");
		String name = evt.getPropertyName();
		if (PropertyChangeNames.STATE_CHANGE.toString().equals(name)) {
			State s = (State) evt.getNewValue();
			jTxtFldToggleState.setText(s.toString());
		}
		repaint();
	}

	// All controls init:ed here
	private void initCtrl() {
		Action service = ControlFactory
				.getControl(ControlFactory.Ctrl.SERVICE_CTRL);
		jBtnService.setAction(service);
		// Will use this as input in ServiceCtrl (action is like a Map)
		// A bit ugly using strings as keys
		service.putValue("MainFrame.jTextFieldIn", jTextFieldIn);

		// Get other control
		Action state = ControlFactory
				.getControl(ControlFactory.Ctrl.STATE_CTRL);
		jBtnToggle.setAction(state);
	}

	// The below is auto generated. Never touch !!!-----------------------------

	private void initGUI() {
		try {
			GridLayout thisLayout = new GridLayout(3, 2);
			thisLayout.setHgap(5);
			thisLayout.setVgap(5);
			thisLayout.setColumns(2);
			thisLayout.setRows(3);
			getContentPane().setLayout(thisLayout);
			this.setTitle("A GENERIC APPLICATION");
			this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			{
				jLabelIn = new JLabel();
				getContentPane().add(jLabelIn);
				jLabelIn.setText("Service call in/out");
			}
			{
				jTextFieldIn = new JTextField();
				getContentPane().add(jTextFieldIn);
				jTextFieldIn.setColumns(10);
			}
			{
				jPanelDummy = new JPanel();
				getContentPane().add(jPanelDummy);
			}
			{
				jBtnService = new JButton();
				getContentPane().add(jBtnService);
				jBtnService.setName("jBtnService");
			}
			{
				jBtnToggle = new JButton();
				getContentPane().add(jBtnToggle);
				jBtnToggle.setText("Toggle Model State");
			}
			{
				jTxtFldToggleState = new JTextField();
				getContentPane().add(jTxtFldToggleState);
				jTxtFldToggleState.setColumns(10);
			}
			pack();
			this.setSize(300, 100);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
